package cn.hzw.supermarket.activity;

import static cn.hzw.supermarket.util.HttpUtil.sendHttpRequest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityModifyPswBinding;
import cn.hzw.supermarket.databinding.FragmentMeBinding;
import cn.hzw.supermarket.fragment.MeFragment;
import cn.hzw.supermarket.model.ResultInfo;
import cn.hzw.supermarket.util.Md5Utils;


public class ModifyPswActivity extends AppCompatActivity {
    private ActivityModifyPswBinding binding;
    private String username;
    private String psw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏，隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityModifyPswBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        binding.layoutTitle.tvTitle.setText("修改密码");
        binding.layoutTitle.ivBack.setOnClickListener(view -> {
            finish();
        });
        username = LoginActivity.user.getUsername();
        psw = LoginActivity.user.getPassword();
        binding.ivDeleteOriginalPsw.setOnClickListener(v ->{
            binding.etOriginalPsw.setText("");
        });
        binding.ivDeleteNewPsw.setOnClickListener(v ->{
            binding.etNewPsw.setText("");
        });
        binding.ivDeleteNewPswAgain.setOnClickListener(v ->{
            binding.etNewPswAgain.setText("");
        });
        binding.save.setOnClickListener(view -> {
            Log.d("TAG", "dfghhdfg:"+psw);
            String oldPsw = binding.etOriginalPsw.getText().toString();
            String newPsw = binding.etNewPsw.getText().toString();
            String newPswAgain = binding.etNewPswAgain.getText().toString();
            if (oldPsw.isEmpty()){
                Toast.makeText(this, "原密码不能为空", Toast.LENGTH_SHORT).show();
            }else if (newPsw.isEmpty()||newPswAgain.isEmpty()){
                Toast.makeText(this, "新密码不能为空", Toast.LENGTH_SHORT).show();
            }else if (newPsw.equals(oldPsw)){
                Toast.makeText(this, "新密码不能和原密码一致", Toast.LENGTH_SHORT).show();
            }else if (!newPsw.equals(newPswAgain)){
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            }else if (!psw.equals(Md5Utils.getMd5Password(oldPsw))){
                Toast.makeText(this, "原密码输入错误", Toast.LENGTH_SHORT).show();
            }else {
                postRequest(newPsw,LoginActivity.user.getId());
//                Intent intent = new Intent(this,LoginActivity.class);
//                startActivity(intent);
                Log.d("TAG", "findViews: 11111111");
//                    Intent intent = new Intent(this,Loading3Activity.class);
//                    startActivity(intent);
            }
        });

    }
    ResultInfo resultInfo;
    private void postRequest(String password, Integer id) {
        new Thread(() -> {
            Map<String, String> map = new HashMap<>();
            map.put("password", password);
            map.put("id", String.valueOf(id));
            String response = sendHttpRequest("/user/updatePwd", map);
            Log.d("TAG", "postRequest: "+response);
            if(response!=null&&!response.isEmpty()){
                resultInfo = JSONObject.parseObject(response, ResultInfo.class);//将JSON字符串转换成对象

              if(resultInfo.isSuccess()){
//                    user=resultInfo.getUser();
//                    userOut = resultInfo.getUser();
                    Intent intent=new Intent(this,LoginActivity.class);
                    startActivity(intent);
                } else
                    runOnUiThread(() -> Toast.makeText(this, "修改失败", Toast.LENGTH_LONG).show());
            }
        }).start();
    }
}